/**************************************************************
 *@ProjectName: blweibo
 *@FileName: MBlogListItemView.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhiweibo.models.Count;
import com.zhiweibo.models.Topic;
import com.zhiweibo.models.User;
import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.TopicUtil;

/**
 * The Class MBlogListItemView.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class MBlogListItemView extends LinearLayout implements OnClickListener {

    /** The m topic. */
    private Topic mTopic;

    /** The tv name. */
    private TextView tvName;

    /** The tv date. */
    private TextView tvDate;

    /** The m tv content. */
    private TextView mTvContent;

    /** The m view. */
    private View mView;

    /** The tv sub content. */
    private TextView tvSubContent;

    /** The iv portrait. */
    private ImageView ivPortrait;

    /** The tweet_upload_pic1. */
    private ImageView tweet_upload_pic1;

    /** The tweet_upload_pic2. */
    private ImageView tweet_upload_pic2;

    /** The tweet_redirect_pic. */
    private ImageView tweet_redirect_pic;

    /** The tweet_comment_pic. */
    private ImageView tweet_comment_pic;

    /** The tweet_redirect. */
    private TextView tweet_redirect;

    /** The tweet_comment. */
    private TextView tweet_comment;

    /** The tv form. */
    private TextView tvForm;

    /** The context. */
    private Context context;

    /** The m count. */
    private Count mCount;

    /** The task1. */
    private AsyncTask task1;

    /** The task2. */
    private AsyncTask task2;

    /** The task3. */
    private AsyncTask task3;

    /** The task4. */
    private AsyncTask task4;

    /** The iv v. */
    private ImageView ivV;

    /** The iv hr. */
    private ImageView ivHR;

    /** The iv li. */
    private ImageView ivLi;

    /** The iv qi. */
    private ImageView ivQi;

    /**
     * Instantiates a new m blog list item view.
     * 
     * @param context1
     *            the context1
     * @param topic
     *            the topic
     */
    public MBlogListItemView(Context context1, Topic topic) {
        super(context1);
        context = context1;
        mTopic = topic;
        ((LayoutInflater) context.getSystemService("layout_inflater")).inflate(R.layout.itemview, this);
        tvName = (TextView) findViewById(R.id.tvItemName);
        tvDate = (TextView) findViewById(R.id.tvItemDate);
        mTvContent = (TextView) findViewById(R.id.tvItemContent);
        mView = findViewById(R.id.subLayout);
        ivPortrait = (ImageView) findViewById(R.id.ivItemPortrait);
        tvSubContent = (TextView) findViewById(R.id.tvItemSubContent);
        tweet_upload_pic1 = (ImageView) findViewById(R.id.tweet_upload_pic1);
        tweet_upload_pic2 = (ImageView) findViewById(R.id.tweet_upload_pic2);
        tvForm = (TextView) findViewById(R.id.tweet_form);
        tweet_redirect_pic = (ImageView) findViewById(R.id.tweet_redirect_pic);
        tweet_comment_pic = (ImageView) findViewById(R.id.tweet_comment_pic);
        tweet_redirect = (TextView) findViewById(R.id.tweet_redirect);
        tweet_comment = (TextView) findViewById(R.id.tweet_comment);
        tweet_redirect_pic.setOnClickListener(this);
        tweet_comment_pic.setOnClickListener(this);
        tweet_redirect.setOnClickListener(this);
        tweet_comment.setOnClickListener(this);
        ivV = (ImageView) findViewById(R.id.ivItemV);
        ivHR = (ImageView) findViewById(R.id.ivItemHR);
        ivLi = (ImageView) findViewById(R.id.ivItemLi);
        ivQi = (ImageView) findViewById(R.id.ivItemQi);
        setUI();
    }

    /**
     * Sets the ui.
     */
    private void setUI() {
        User user = mTopic.getUser();
        tvName.setText(user.getUsername());
        TextPaint tp = tvName.getPaint();
        tp.setFakeBoldText(true);
        tvDate.setText(mTopic.getDateline());
        tvForm.setText(TopicUtil.formatFrom(mTopic.getFrom()));
        int iSize = SettingsPref.getTextSize(getContext());
        mTvContent.setTextSize(iSize);
        tvSubContent.setTextSize(iSize);
        mTvContent.setText(mTopic.getHText(), TextView.BufferType.SPANNABLE);
        TopicUtil.highlightContent(getContext(), (Spannable) mTvContent.getText(), false);

        if (user.getV()) {
            ivV.setVisibility(View.VISIBLE);
        }
        if (user.getHR()) {
            ivHR.setVisibility(View.VISIBLE);
        }
        if (user.getL()) {
            ivLi.setVisibility(View.VISIBLE);
        }
        if (user.getQ()) {
            ivQi.setVisibility(View.VISIBLE);
        }

        String retopid = mTopic.getRetopic().getTid();
        if (!TextUtils.isEmpty(retopid)) {
            mView.setVisibility(View.VISIBLE);
            tvSubContent.setText("@" + mTopic.getRetopic().getUser().getUsername() + ":"
                    + mTopic.getRetopic().getHText(), TextView.BufferType.SPANNABLE);
            TopicUtil.highlightContent(getContext(), (Spannable) tvSubContent.getText(), false);
        } else {
            mView.setVisibility(View.GONE);
        }
        task3 = new MBlogTask(ivPortrait, user.getFace()).execute(0);
        String pic_s = mTopic.getPic_s();
        if (!TextUtils.isEmpty(pic_s)) {
            tweet_upload_pic1.setVisibility(View.VISIBLE);
            task1 = new MBlogTask(tweet_upload_pic1, pic_s).execute(0);
            tweet_upload_pic1.setOnClickListener(this);
        } else {
            tweet_upload_pic1.setVisibility(View.GONE);
        }
        String pic_s2 = mTopic.getRetopic().getPic_s();
        if (!TextUtils.isEmpty(pic_s2)) {
            tweet_upload_pic2.setVisibility(View.VISIBLE);
            task2 = new MBlogTask(tweet_upload_pic2, pic_s2).execute(0);
            tweet_upload_pic2.setOnClickListener(this);
        } else {
            tweet_upload_pic2.setVisibility(View.GONE);
        }
        task4 = new CountTask().execute(0);
    }

    /**
     * Sets the count.
     */
    private void setCount() {
        if (mCount.getReplys() > 0) {
            tweet_redirect_pic.setVisibility(View.VISIBLE);
            tweet_redirect.setVisibility(View.VISIBLE);
            tweet_redirect.setText(mCount.getReplys() + "");
        }
        if (mCount.getComments() > 0) {
            tweet_comment_pic.setVisibility(View.VISIBLE);
            tweet_comment.setVisibility(View.VISIBLE);
            tweet_comment.setText(mCount.getComments() + "");
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        if (v == tweet_comment || v == tweet_comment_pic) {
            Intent intent = new Intent(getContext(), EditActivity.class);
            intent.putExtra("mode", 1);
            intent.putExtra("topicId", mTopic.getTid());
            context.startActivity(intent);
        } else if (v == tweet_redirect_pic || v == tweet_redirect) {
            Intent intent = new Intent(getContext(), EditActivity.class);
            intent.putExtra("mode", 2);
            intent.putExtra("topicId", mTopic.getTid());
            if (!TextUtils.isEmpty(mTopic.getRetopic().getTid())) {
                intent.putExtra("content", "//@" + mTopic.getUser().getUsername() + ":" + mTopic.getHText());
            }
            context.startActivity(intent);
        } else if (v == tweet_upload_pic2) {
            viewBigsizeBmp(mTopic.getRetopic().getPic_o(), getResources().getDrawable(R.drawable.loading));
        } else if (v == tweet_upload_pic1) {
            viewBigsizeBmp(mTopic.getPic_o(), getResources().getDrawable(R.drawable.loading));
        }
    }

    /**
     * Update.
     * 
     * @param context
     *            the context
     * @param topic
     *            the topic
     */
    public void update(Context context, Topic topic) {
        mTopic = topic;
        tweet_redirect_pic.setVisibility(View.GONE);
        tweet_redirect.setVisibility(View.GONE);
        tweet_comment_pic.setVisibility(View.GONE);
        tweet_comment.setVisibility(View.GONE);
        ivPortrait.setImageResource(R.drawable.portrait);
        tweet_upload_pic2.setImageResource(R.drawable.loading);
        tweet_upload_pic1.setImageResource(R.drawable.loading);
        ivV.setVisibility(View.GONE);
        ivHR.setVisibility(View.GONE);
        ivLi.setVisibility(View.GONE);
        ivQi.setVisibility(View.GONE);
        if (task1 != null && task1.getStatus() == AsyncTask.Status.RUNNING) {
            task1.cancel(true);
        }
        if (task2 != null && task2.getStatus() == AsyncTask.Status.RUNNING) {
            task2.cancel(true);
        }
        if (task3 != null && task3.getStatus() == AsyncTask.Status.RUNNING) {
            task3.cancel(true);
        }
        if (task4 != null && task4.getStatus() == AsyncTask.Status.RUNNING) {
            task4.cancel(true);
        }
        setUI();
    }

    /**
     * View bigsize bmp.
     * 
     * @param strUrl
     *            the str url
     * @param paramDrawable
     *            the param drawable
     */
    private void viewBigsizeBmp(String strUrl, Drawable paramDrawable) {
        ImageViewerDialog localImageViewerDialog = new ImageViewerDialog(context, strUrl, paramDrawable);
        localImageViewerDialog.setCanceledOnTouchOutside(true);
        localImageViewerDialog.show();
        localImageViewerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface paramDialogInterface) {
            }
        });
    }

    /**
     * The Class MBlogTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class MBlogTask extends AsyncTask<Object, Integer, Bitmap> {

        /** The piv. */
        private ImageView piv;

        /** The str url. */
        private String strUrl;

        /**
         * Instantiates a new m blog task.
         * 
         * @param iv
         *            the iv
         * @param url
         *            the url
         */
        public MBlogTask(ImageView iv, String url) {
            piv = iv;
            strUrl = url;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Bitmap doInBackground(Object... params) {
            return HttpUtil.getImage(getContext(), strUrl);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Bitmap result) {
            piv.setImageBitmap(result);
        }
    }

    /**
     * The Class CountTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class CountTask extends AsyncTask {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            mCount = HttpUtil.getcount(mTopic.getTid());
            return mCount;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            setCount();
        }

    }
}
